package cn.yunhe.servlet;

import cn.yunhe.util.RandomUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/GetQrcode")
public class GetQrcode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sumstr = req.getParameter("sum");
        System.out.println(sumstr);
        PrintWriter out=resp.getWriter();
        if (sumstr.equals("0")) {
            out.print(0);
            out.flush();
            out.close();
        } else {
            int sum= Integer.parseInt(sumstr);
            AlipayClient alipayClient = new DefaultAlipayClient(
                    "https://openapi.alipaydev.com/gateway.do",
                    "2016091800539794",
                    "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCbD7KJCRbHW3sYgybjGzt6QIwHxT42pdg1aYPD/1fy6/N/GdI83bAyUrlQyRAPDvNPgZFJMJlY2KJfbKICmyBxVyXHQeedsmz47Ucrh4gNIT8JdCuHLzFHnX99c0I+K4lggil4tOPD5i/mgTxDmNstcjb/5896xKtNRxy/RbnJBGDT49MCSU8rSJMUGvByVyHpYKO7ENbc96xC5T1s1JnzG2hLmUx7ty8EapCQZEgjBF9GNV59rQSxbJ15q3eIhPMw3Po7Dy+vcQ7xM9TpOTgkS7VjoLdrvJKs6pxT+hxmuBCsIhVoBOwJgHkAHO+ZuIDXR5H3wuHeIKnsl/xJh9g/AgMBAAECggEAVi8xYm1E1eWI06UB1jTqqBZ8Pk1pOOKQ+478B9JCxehTXI+zhNUK+81y5u36GQSMQ+KWKAH1Gaa/CR7OJXinXZe8jbWZ0NK9MtZfOliITBPwf4asMuUOFOn7VHTdmsSEBLo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L8pfhI5PWbB9I4Q==",
                    "json",
                    "UTF-8",
                    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxcaauMWrLq/6flsFR2VeoJdjl7AzwSOjb3OvW5z1U01WenXsra3sVRuGeShoAnIWufQfby281ZPuFpYYp8F1eAao+/Oy8X6d52JKxfK6CvLnCzOiH9o9f3fFmz2y11ReSYY/1mpj2to+wEeRqPAbG//Islt9J53t1uoNEYjcfScGib33/dnkx5UZymziXIFwOT1cJf/IlNcSmMr03daGiGDn8EobenN2ZprIyHg2LuN5EJGHTPhyKcrgOT9PSmMP3FxGuLKS+FYsnyEzowwjZORSNBScsnXWUe9MA5yAT8wFjScLzAKhxphPCSRInQGNBrFXyWqFO+w1Xv+4vNVzdQIDAQAB",
                    "RSA2");
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
            request.setBizContent("{" +
                    "    \"out_trade_no\":\"" + RandomUtil.getRandom() + "\"," +
                    "    \"total_amount\":\"" + sum + "\"," +
                    "    \"subject\":\"Iphone6 16G\"," +
                    "    \"store_id\":\"NJ_001\"," +
                    "    \"timeout_express\":\"10m\"}");//设置业务参数
            AlipayTradePrecreateResponse response = null;
            try {
                response = alipayClient.execute(request);
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            String a = response.getBody();
            String[] arr = a.split(",");
            String qrcode = null;
            qrcode = arr[3];
            qrcode = qrcode.substring(11, qrcode.length() - 2);
            qrcode = qrcode.replace("\\", "");
            qrcode = "http://qr.liantu.com/api.php?bg=f3f3f3&fg=ff0000&gc=222222&el=l&w=200&m=10&text=" + qrcode;

            System.out.println(qrcode);
            out.print(qrcode);
            out.flush();
            out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
